mini.parse();

function CouponHandler() {
};

CouponHandler.instance = new CouponHandler();

CouponHandler.isQueryBtnExpended = false;

//获得全局的购物券数据表格对象
CouponHandler.couponGrid = mini.get("couponGrid");

//加载表格数据
CouponHandler.couponGrid.load();

//品牌状态数据
CouponHandler.couponStateData = [
	{text: '全部', id: -1},
	{text: '无效', id: 0},
	{text: '有效', id: 1}
];

mini.get("query_bciState").setData(CouponHandler.couponStateData);

/**
 * 根据用户权限展示功能按钮
 */
CouponHandler.loadToolbar = function() {
	var array = new Array("coupon_list_add", "coupon_list_edit", "coupon_list_del","coupon_list_query","coupon_list_refresh");
	var userArr = MiniuiTools.isHasPurview(array);
	for ( var i = 0; i < userArr.length; i++) {
		mini.get(array[i]).setVisible(userArr[i]);
	}
};
CouponHandler.loadToolbar();

/**
 * 弹出添加购物券窗口
 */
CouponHandler.addCoupon = function() {
	miniWin.afreshNewWin();
	miniWin.setParams(500, 380, "添加购物券");
	miniWin.setOption("bodyStyle","padding:0px;");
	miniWin.setOption("showMaxButton",true);
	miniWin.setOnButtonClick();

	miniWin.load("couponForm.jsp",function(){
		var iframe = miniWin.getIFrameEl();
        var data = {action: "new"};
        iframe.contentWindow.CouponFormHandler.passData(data);
        miniWin.setOnMax(iframe.contentWindow.CouponFormHandler.onMaxAction);
	},function(action){
		CouponHandler.couponGrid.reload();
	});
	
	miniWin.show();
};

/**
 * 弹出编辑购物券窗口
 */
CouponHandler.editCoupon = function() {
	var rowData = CouponHandler.couponGrid.getSelected();
	var rowsData = CouponHandler.couponGrid.getSelecteds();
	
	if(rowsData.length == 1){
		miniWin.afreshNewWin();
		miniWin.setParams(500, 380, "编辑购物券[<font color='blue'>"	+ rowData.bciName + "</font>]");
		miniWin.setOption("bodyStyle","padding:0px;");
		miniWin.setOption("showMaxButton",true);
		miniWin.setOnButtonClick();
		
		miniWin.load("couponForm.jsp",function(){
			var iframe = miniWin.getIFrameEl();
            var data = {action: "edit", bciId: rowData.bciId};
            iframe.contentWindow.CouponFormHandler.passData(data);
            miniWin.setOnMax(iframe.contentWindow.CouponFormHandler.onMaxAction);
		},function(action){
			CouponHandler.couponGrid.reload();
		});
		
		miniWin.show();
	}else if (rowsData.length > 1){
		MiniuiTools.miniWarning("系统提示","不能同时编辑多条资源！");
	}else {
		MiniuiTools.miniWarning("系统提示","请选择要编辑的资源！");
	}
	
};

/**
 * 删除购物券资源
 */
CouponHandler.delCoupon = function() {
	var rowsData = CouponHandler.couponGrid.getSelecteds();
	
	if(rowsData.length > 0){
		mini.confirm("确认删除选择的" + rowsData.length + "条记录？" ,"系统提示",
			function(action, value){
				if (action == "ok") {
					var ids = "";
					
					for ( var i = 0; i < rowsData.length - 1; i++){
						ids += rowsData[i].bciId+ ",";
					}
					ids += rowsData[rowsData.length - 1].bciId;
					
					CouponHandler.couponGrid.loading();
					
                    $.ajax({
                        url: __root + "/delCouponByIds.do",
                        data: {
				        	bciIds: ids,
				        	lslUser: gbOperateUser,
				    		lslAddress: gbOperateIp
				        },
                        success: function (text) {
                        	MiniuiTools.miniInfo("系统提示","操作成功！", function(action){
				        		CouponHandler.couponGrid.reload();
				        	});
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                        	MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
                        },
                        complete: function(){
                        	CouponHandler.couponGrid.unmask();
                        }
                    });	
                } 
			}
		)
	}else{
		MiniuiTools.miniWarning("系统提示","请选择要删除的资源！");
	}
};

/**
 * 更新购物券状态
 */
CouponHandler.updateCouponState = function() {
	if (!MiniuiTools.hasPurview("coupon_list_edit")) {
		return;
	}
	
	var rowData = CouponHandler.couponGrid.getSelected();
	var bciId = rowData.bciId;
	
	CouponHandler.couponGrid.loading();
	
    $.ajax({
        url: __root + "/updateCouponStateById.do",
        data: {
        	bciId: bciId,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        success: function (text) {
            CouponHandler.couponGrid.reload();
        },
        error: function (jqXHR, textStatus, errorThrown) {
            MiniuiTools.miniError("系统提示", "操作失败！" + jqXHR.responseText);
        },
        complete: function(){
        	CouponHandler.couponGrid.unmask();
        }
    });	
};

/**
 * 展开或折叠查询购物券面板
 */
CouponHandler.queryCoupon = function() {
	if (CouponHandler.isQueryBtnExpended) {
		CouponHandler.isQueryBtnExpended = false;
		mini.get("coupon_list_query").setText("展开查询");
		mini.get("coupon_list_query").setIconCls("icon-btn-expand");
	} else {
		CouponHandler.isQueryBtnExpended = true;
		mini.get("coupon_list_query").setText("折叠查询");
		mini.get("coupon_list_query").setIconCls("icon-btn-collapse");
	}
	
	$("#queryCouponDiv").toggle(500);
    setTimeout(function(){mini.get("couponPanel").doLayout();},600);
};

/**
 * 执行查询购物券的方法
 */
CouponHandler.doQueryCoupon = function(){
	var bciName = mini.get("query_bciName").getValue();
	var bciState = mini.get("query_bciState").getValue();
	
    CouponHandler.couponGrid.load({bciName: bciName, bciState: bciState});
};

/**
 * 刷新页面数据及布局
 */
CouponHandler.afresh = function(){
	CouponHandler.doQueryCoupon();
	mini.get("couponPanel").doLayout();
}

/**
 * DrawCell 绘制单元格
 */
CouponHandler.couponGrid.on("drawcell", function(e) {
	 var record = e.record;
     var column = e.column;
     var field = e.field;
     var value = e.value;
     
     if (column.field == "bciPicurl") {
        if (e.value != "") {
            e.cellHtml = "<img src='" + __root + "/imgViewerContorller.do?basePath=" + __realpath + __couponImgPath + "&newImgPath=" + e.value + "'/>";                    
        } else {
            e.cellHtml = "";
        }
    }
     
     if (column.field == "bciState") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='CouponHandler.updateCouponState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='CouponHandler.updateCouponState()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
     }
});

